package com.baihui.taobao.db;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * Created by Kevin. User: HSZM1106-0312 Date: 12-1-3 Time: 下午8:31
 */
public class DBConnectionMgr {

	private static DBConnectionMgr _instance;
	private String jdbcurl;
	private String username;
	private String password;
	private boolean init;
	private static DataSource ds = null;

//	public static DBConnectionMgr getInstance() {
//		if (_instance == null) {
//			_instance = new DBConnectionMgr();
//		}
//		return _instance;
//	}

	public boolean isInit() {
		return init;
	}

	// public Connection getConnection() throws Exception {
	// String jdbcdriverl = "com.mysql.jdbc.Driver";
	// try {
	// Class.forName(jdbcdriverl);
	// return DriverManager.getConnection(jdbcurl, username, password);
	// } catch (ClassNotFoundException e) {
	// throw new Exception(e);
	// } catch (SQLException e) {
	// throw new Exception(e);
	// }
	// }

	public static Connection getConnection() throws NamingException,
			SQLException {
		if (ds == null) {
			Context ctx = new InitialContext();
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/taobaodata");
		}
		return ds.getConnection();
	}

//	public void init(String filename) throws Exception {
//		try {
//			File file1 = new File(filename);
//			FileInputStream fileInputStream = new FileInputStream(file1);
//			Properties properties = new Properties();
//			properties.load(fileInputStream);
//			this.jdbcurl = properties.getProperty("jdbcurl");
//			this.username = properties.getProperty("username");
//			this.password = properties.getProperty("password");
//			this.init = true;
//		} catch (Exception e) {
//			throw new Exception(e);
//		}
//	}
}
